package org.demo.filemanager;

import org.boxlayout.gui.BoxLayoutEngine;
import org.boxlayout.gui.VisualElement;
import org.boxlayout.gui.layout.LayoutBase;
import org.boxlayout.gui.type.ColorEnum;
import org.boxlayout.gui.widget.TreeBrowsable;
import org.demo.filemanager.widget.filesystem.DirectoryFileWidget;
import org.demo.filemanager.widget.filesystem.FileWidget;

public class TreeNavigator {

	TreeBrowsable currentNode = null;
	BoxLayoutEngine ble = null;
	VisualElement visual = null;
	public TreeNavigator(BoxLayoutEngine ble) {
		this.ble = ble;
	}

	public void setCurrentNode(TreeBrowsable node) {
		this.currentNode = node;
		VisualElement vis = node.getVisual();
		
		//visual.setX(vis.getAbsoluteX()-this.visual.getWidth());
		//visual.setY(vis.getAbsoluteYCenter()+(visual.getHeight()/2));
		//visual.setZ(vis.getAbsoluteZ()+vis.getDeep());
		Float border = 0.01f;
		visual.setX(vis.getAbsoluteX()-border);
		visual.setY(vis.getAbsoluteY()+border);
		visual.setZ(vis.getAbsoluteZ()+LayoutBase.Z_SPACE);
		visual.setPreferedWidth(vis.getWidth()+(2*border));
		visual.setPreferedHeight(vis.getHeight()+(2*border));
		ble.reLayout();
		
		//visual.setText(vis.getDeep().toString());
		
	}
	
	public void goToPrevious(){
    	System.out.println("goToPrevious");
		TreeBrowsable brother = currentNode.getPreviousBrother();
        if(brother!=null){
        	currentNode.setSelected(false);
            setCurrentNode(brother);
        }
    }
    
    public void goToNext(){
    	System.out.println("goToNext");
		TreeBrowsable brother = currentNode.getNextBrother();
        if(brother!=null){
        	currentNode.setSelected(false);
        	setCurrentNode(brother);
        }
    }
    
    public void goToParent() {
    	System.out.println("goToParent");
    	TreeBrowsable parent = currentNode.getParent();
    	if(parent!=null){
    		parent.setExpanded(false);
    		setCurrentNode(parent);
    		if(parent.getParent()!=null){
    			parent.getParent().focus();
    		}
    	}
	}
    public void goToFirstChild() {
    	System.out.println("right expand");
    	
    	currentNode.setExpanded(true);
    	if(!currentNode.getChilds().isEmpty()){			
    		currentNode.focus();
    		TreeBrowsable first = currentNode.getChilds().get(0);
    		currentNode.setSelected(false);
    		setCurrentNode(first);
    	}
    }

    public void goBack() {
    	System.out.println("goBack");
    	if (currentNode instanceof DirectoryFileWidget) {
    		DirectoryFileWidget dfw = (DirectoryFileWidget) currentNode;
    		dfw.setExpanded(false);
    		TreeBrowsable parent = dfw.getParent();
    		if(parent!=null){
    			setCurrentNode(parent);
    			
    			parent.focus();
    		}else if(dfw.isRoot()){
    			dfw.setRoot(true);
    		}

    	}else if (currentNode instanceof FileWidget) {
    		FileWidget vf = (FileWidget) currentNode;
    		TreeBrowsable parent = vf.getParent();             
    		if(parent!=null){
    			setCurrentNode(parent);
    			parent.focus();
    		}else if(vf.isRoot()){
    			vf.focus();
    		}
    	}
    }

	public void setVisual(VisualElement selectArea) {
		visual = selectArea;
		visual
		//.setText(">")
		.setColor(ColorEnum.BLUE).setAlpha(0.5f);
		
	}

}
